package com.taomu.app.zongchouhuli.utils;

import java.math.BigDecimal;
import java.util.Locale;

/**
 * 价格工具
 *
 * @author mili
 * @createTime 2016/10/19
 */
public class PriceUtil {


    public static String format(long price) {
        double p = price / 100d;
        return format2(p);
    }

    public static String formatMilii(long price){
        double p = price/1000000f;
        return format2(p);
    }

    public static String formatDis(long dis){
        double p = dis / 1000f;
        return format2(p);
    }

    public static String formatDiscount(long dis){
        double p = dis / 1000f;
        if(p>=10||p<=0){
            return "";
        }
        return keep1DecimalStr(p);
    }

    public static String formatCount(long dis){
        double p = dis / 10000f;
        return keep1DecimalStr(p);
    }

    /**
     * 格式化价格，通用格式
     *
     * @param price 价格
     */
    public static String format(double price) {
//        int value = (int) price;
//        if(value == price){
//            return String.valueOf(value);
//        }
        return String.format(Locale.getDefault(), "%.02f", keep2Decimal(price));
    }

    /**
     * 格式化价格，通用格式
     *
     * @param price 价格
     */
    public static String format2(double price) {
        int value = (int) price;
        if(value == price){
            return String.valueOf(value);
        }
        return String.format(Locale.getDefault(), "%.02f", keep2Decimal(price));
    }

    /**
     * 四舍五入保留两位小数
     * @param price
     * @return
     */
    public static String keep2DecimalStr(double price){
        return  String.format(Locale.getDefault(), "%.02f", keep2Decimal(price));
    }

    /**
     * 格式化价格，只保留整数
     *
     * @param price 价格
     */
    public static String formatInt(double price) {

        return String.format(Locale.getDefault(), "%.0f", price);
    }

    /**
     * 格式化价格，只保留整数
     *
     * @param price 价格
     */
    public static String keep1DecimalStr(double price) {
        int value = (int) price;
        if(value == price){
            return String.valueOf(value);
        }
        return String.format(Locale.getDefault(), "%.1f", price);
    }

    /**
     * 四舍五入保留两位小数
     * @param d
     * @return
     */
    public static double keep2Decimal(double d){
        BigDecimal b   =   new BigDecimal(String.valueOf(d));
        return  b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static void main(String[] args) {
        System.out.println(formatDiscount(8000));
    }
}
